package com.ape.mapsa.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

import com.ape.mapsa.model.Role;
import com.ape.mapsa.model.UserRole;
import com.ape.mapsa.produccion.service.IProduccionService;

@ManagedBean(name = "menuMB")
@ViewScoped
public class MenuBean implements Serializable {

	private static final long serialVersionUID = 8127573661966351346L;
	private static final Logger log = Logger.getLogger(MenuBean.class);
	private MenuModel model;

	@ManagedProperty(value = "#{userMB}")
	private UserBean userBean;

	@ManagedProperty("#{produccionService}")
	private IProduccionService prodService;

	private List<Role> roles = new ArrayList<Role>();

	@PostConstruct
	public void init() {
		log.info("MENU BEAN CONSTRUCTED");
		List<UserRole> userRoles = userBean.getRoles();
		for (UserRole userRole : userRoles) {
			roles.add(userRole.getRole());
		}

		if (model == null) {
			model = new DefaultMenuModel();

			MenuItem item = new MenuItem();
			if (userBean == null) {
				log.info("USER NULL");
			}

			Submenu submenu = new Submenu();
			submenu.setLabel("Administrador");

			item.setValue("Administracion de usuarios");
			item.setUrl("/pages/userAdmin.xhtml");
			submenu.getChildren().add(item);
			if (roles.contains(new Role("mapsa-admin"))) {
				model.addSubmenu(submenu);
			}

			// PRODUCCION
			submenu = new Submenu();
			submenu.setLabel("Producción");

			// ORDENES DE TRABAJO
			item = new MenuItem();
			item.setValue("Ordenes de trabajo");
			item.setId("ordenDeProdMenuItem");
			item.setUrl("/pages/ordenProduccion.xhtml");
			item.setDisabled(!roles.contains(new Role("mapsa-jefe-prod")) && !roles.contains(new Role("mapsa-admin")));
			submenu.getChildren().add(item);

			// COLORES
			item = new MenuItem();
			item.setValue("Colores");
			item.setUrl("/pages/addColores.xhtml");
			item.setDisabled(!roles.contains(new Role("mapsa-jefe-prod")) && !roles.contains(new Role("mapsa-admin")));
			submenu.getChildren().add(item);

			// ITEMS
			item = new MenuItem();
			item.setValue("Items");
			item.setUrl("/pages/articulos.xhtml");
			item.setDisabled(!roles.contains(new Role("mapsa-jefe-prod")) && !roles.contains(new Role("mapsa-admin")));
			submenu.getChildren().add(item);
			model.addSubmenu(submenu);

			submenu = new Submenu();
			submenu.setLabel("Ventas");

			// ORDENES DE VENTA
			item = new MenuItem();
			item.setValue("Ordenes de venta");
			item.setUrl("/pages/pedidosVenta.xhtml");
			item.setDisabled(!roles.contains(new Role("mapsa-ventas")) && !roles.contains(new Role("mapsa-admin")));
			submenu.getChildren().add(item);

			// MUESTRAS
			item = new MenuItem();
			item.setValue("Muestras");
			item.setUrl("/pages/addMuestras.xhtml");
			item.setDisabled(!roles.contains(new Role("mapsa-ventas")) && !roles.contains(new Role("mapsa-admin")));
			submenu.getChildren().add(item);

			// CLIENTES
			item = new MenuItem();
			item.setValue("Clientes");
			item.setUrl("/pages/addClientes.xhtml");
			item.setDisabled(!roles.contains(new Role("mapsa-ventas")) && !roles.contains(new Role("mapsa-admin")));
			submenu.getChildren().add(item);
			model.addSubmenu(submenu);
		}

	}

	public MenuModel getModel() {
		return model;
	}

	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	@PreDestroy
	public void predestroy() {
		log.info("MENU BEAN IS GOING TO BE DESTROYED");
	}

	public IProduccionService getProdService() {
		return prodService;
	}

	public void setProdService(IProduccionService prodService) {
		this.prodService = prodService;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

}
